package com.byzk.p2p.admin.web.controller;

import java.util.Date;

import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.web.dto.Banner;
import com.byzk.p2p.admin.web.service.BannerService;
import com.byzk.p2p.admin.web.vo.BannerVo;

/**
 * Banner
 * 
 * @author LJY
 *
 */
@Controller
@RequestMapping("/admin/banner")
public class BannerController extends AdminBaseController {
	@Autowired
	private BannerService bannerService;

	@RequestMapping("/list")
	public String listBanner(Model model, @ModelAttribute BannerVo vo) {
		PageInfo<BannerVo> pageInfo = bannerService.bannerList(vo);
		model.addAttribute("pageInfo", pageInfo);
		return ftl(Thread.currentThread().getStackTrace());
	}

	/**
	 * 保存banner
	 * @param banner
	 * @return
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public void saveBanner(@RequestBody Banner banner) {
		try {
			if (getUserId() == null) {
				super.printJSON(new JsonMsg(0, "请先登录！"));
				return;
			}
			if (banner != null) {
				if(StringUtil.isBlank(banner.getTitle())){
					printJSON(new JsonMsg(1, "标题不能为空"));
					return;
				}
				if(StringUtil.isBlank(banner.getLinkUrl())){
					printJSON(new JsonMsg(2, "Banner链接不能为空"));
					return;
				}
				if(banner.getResourceId() == null){
					printJSON(new JsonMsg(3, "Banner图片不能为空"));
					return;
				}
				if(banner.getOrderIndex() == null){
					printJSON(new JsonMsg(4, "Banner顺序不能为空"));
					return;
				}
				if (StringUtil.isBlank(banner.getTerminal())) {
					printJSON(new JsonMsg(4, "Banner显示终端不能为空"));
					return;
				}
				if(StringUtil.isBlank(banner.getSpecific())){
					printJSON(new JsonMsg(5, "Banner展示规格不能为空"));
					return; 
				}
				banner.setUploadorId(getUserId());
				if (banner.getId() == null) {
					banner.setCreateTime(new Date());
				}
				Integer i = bannerService.saveBanner(banner);
				if (i > 0) {
					super.printJSON(new JsonMsg(-1, "操作成功"));
				} else {
					super.printJSON(new JsonMsg(0, "操作失败"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * banner详细信息
	 * @param model
	 * @param id bannerId
	 * @return
	 */
	@RequestMapping(value="/detailBanner")
	public String detailBanner(Model model,@RequestParam Integer id){
		BannerVo bannerVo = bannerService.getBannerById(id);
		model.addAttribute("model", bannerVo);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	@RequestMapping("/setting")
	public String settingBanner(Model model){
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	/**
	 * 删除banner
	 * @param id
	 */
	@RequestMapping(value="/delBanner",method=RequestMethod.POST)
	public void delBanner(@RequestParam Integer id) throws Exception{
		try {
			Integer i =bannerService.deleteBanner(id);
			if (i > 0) {
				printJSON(new JsonMsg(-1, "删除成功"));
			} else {
				printJSON(new JsonMsg(1, "删除失败"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			printJSON(new JsonMsg(0, "系统异常,请稍后再试！"));
		}
	}
}
